package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.bean.IndexProductItem;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.GridSpacingItemDecoration;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ProductListActivity extends AbsActivity implements View.OnClickListener {
    private RecyclerView rvProductlist;
    private TextView tvComplex,tvNewProduct,tvSales,tvPrice,tvSearchType;
    private ImageView ivBack,ivSearch;
    private EditText etSearch;
    private plAdpter plAdpter;

    private int currentChoose;//0 -> tvComplex 1 -> tvNewProduct 2 -> tvSales 3 -> tvPrice
    private boolean arowUp = false;
    private List<TextView> cotainer = new LinkedList<>();

    private List<IndexProductItem> data = new LinkedList<>();
    private int page = 1;
    private int pagesize = 10;
    private String searchStr;
    private int activityId;
    private int typeId;

    public final static String SearchStr = "search_str";
    public final static String ActivityId = "activity_id";
    public final static String TypeId = "type_id";
    @Override
    public int setView() {
        return R.layout.activity_product_list;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        Intent intent = getIntent();
        searchStr = intent.getStringExtra(SearchStr);
        activityId = intent.getIntExtra(ActivityId,-1);
        typeId = intent.getIntExtra(TypeId,-1);
        if(TextUtils.isEmpty(searchStr) && activityId == -1 && typeId == -1){
            ToastUtil.showToast("All is empty");
        }
        initView();
        initLogic();
        requestData();
    }

    private void requestData() {
        ToastUtil.showToast("currentchoose:"+currentChoose+"   arrowup:"+arowUp);
        IndexProductItem indexProductItem = new IndexProductItem();
        indexProductItem.setProductImgUrl("http://tc.zadtek.com/aaa/images/201407/goods_img/135_P_1405035082640.jpg");
        indexProductItem.setProductTitle("华为 荣耀 畅玩版（白色）真8核 移动版 TD-SCDMA/GSM 双卡双待 豪华套装版");
        indexProductItem.setProductSales("999");
        indexProductItem.setProductStock("666");
        indexProductItem.setProductPrice("￥555.00");
        data.add(indexProductItem);
        data.add(indexProductItem);
        data.add(indexProductItem);
        data.add(indexProductItem);
        BaseResponse<List<IndexProductItem>> response = new BaseResponse<>();
        response.setMessage("");
        response.setCode(0);
        response.setData(data);
        Gson gson = new Gson();
        String js = gson.toJson(response);

//        Map<String, Object> param = NetTool.newParams();
//        param.put("page",page);
//        param.put("pagesize",pagesize);
//        param.put("sortKey",1);
//        param.put("sortOrder",true);
//        NetTool.getApi().getProducts(param)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new BaseObserver<BaseResponse<List<IndexProductItem>>>() {
//                    @Override
//                    public void onData(BaseResponse<List<IndexProductItem>> value) {
//                        if(data.size()>20){
//                            plAdpter.loadMoreEnd();
//                        }else{
//                            data.addAll(value.getData());
//                            plAdpter.notifyDataSetChanged();
//                            plAdpter.loadMoreComplete();
//                        }
//                    }
//                });
        plAdpter.notifyDataSetChanged();
    }

    private void initLogic() {
        tvComplex.setOnClickListener(this);
        tvNewProduct.setOnClickListener(this);
        tvSales.setOnClickListener(this);
        tvPrice.setOnClickListener(this);
        etSearch.setFocusable(false);
        etSearch.setOnClickListener(this);
        ivBack.setOnClickListener(this);

        plAdpter = new plAdpter(R.layout.product_item,data);
        rvProductlist.setAdapter(plAdpter);
        plAdpter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ToastUtil.showToast("click +"+position);
                startActivity(new Intent(ProductListActivity.this,ProductDetailActivity.class));
            }
        });
        plAdpter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                requestData();
            }
        }, rvProductlist);
    }

    private void initView() {
        ivBack = (ImageView) findViewById(R.id.ic_back);
        ivSearch = (ImageView) findViewById(R.id.ic_search);
        ivSearch.setVisibility(View.GONE);
        etSearch =(EditText) findViewById(R.id.et_search);
        Drawable drawable = getResources().getDrawable(R.drawable.ic_search);
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        etSearch.setCompoundDrawables(drawable,null,null,null);
        tvComplex = (TextView) findViewById(R.id.tv_complex);
        tvNewProduct = (TextView) findViewById(R.id.tv_new_product);
        tvSales = (TextView) findViewById(R.id.tv_sales);
        tvPrice = (TextView) findViewById(R.id.tv_price);
        tvSearchType = (TextView) findViewById(R.id.tv_search_type);
        tvSearchType.setVisibility(View.GONE);
        rvProductlist = (RecyclerView) findViewById(R.id.rv_product_list);
        GridLayoutManager layoutManager = new GridLayoutManager(this,2);
        rvProductlist.addItemDecoration(new GridSpacingItemDecoration(2,getResources().getDimensionPixelSize(R.dimen.little_margin), true));
        rvProductlist.setLayoutManager(layoutManager);
        currentChoose = 0;
        cotainer.add(tvComplex);
        cotainer.add(tvNewProduct);
        cotainer.add(tvSales);
        cotainer.add(tvPrice);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_complex:
                setChoose(0);
                break;
            case R.id.tv_new_product:
                setChoose(1);
                break;
            case R.id.tv_sales:
                setChoose(2);
                break;
            case R.id.tv_price:
                setChoose(3);
                break;
            case R.id.ic_back:
                finish();
                break;
            case R.id.et_search:
                finish();
                break;
        }
    }

    private void setChoose(int current) {
        data.clear();
        plAdpter.notifyDataSetChanged();
        page = 1;
        if(current == currentChoose){
            Drawable drawable;
            if(arowUp){
                drawable = getResources().getDrawable(R.drawable.down);
            }else{
                drawable = getResources().getDrawable(R.drawable.up);
            }
            arowUp = !arowUp;
            drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
            cotainer.get(current).setCompoundDrawables(null,null,drawable,null);
            requestData();
            return;
        }
        currentChoose = current;
        arowUp = false;
        tvComplex.setTextColor(getResources().getColor(R.color.color_text_black5nd));
        tvNewProduct.setTextColor(getResources().getColor(R.color.color_text_black5nd));
        tvPrice.setTextColor(getResources().getColor(R.color.color_text_black5nd));
        tvSales.setTextColor(getResources().getColor(R.color.color_text_black5nd));
        tvComplex.setCompoundDrawables(null,null,null,null);
        tvNewProduct.setCompoundDrawables(null,null,null,null);
        tvPrice.setCompoundDrawables(null,null,null,null);
        tvSales.setCompoundDrawables(null,null,null,null);

        Drawable drawable = getResources().getDrawable(R.drawable.down);
        drawable.setBounds(0,0,drawable.getMinimumWidth(),drawable.getMinimumHeight());
        cotainer.get(current).setCompoundDrawables(null,null,drawable,null);
        cotainer.get(current).setTextColor(getResources().getColor(R.color.color_red));
        requestData();
    }

    private class plAdpter extends BaseQuickAdapter<IndexProductItem,BaseViewHolder>{
        public plAdpter(int layoutResId, @Nullable List<IndexProductItem> data) {
            super(layoutResId, data);
        }
        @Override
        protected void convert(BaseViewHolder helper, IndexProductItem item) {
            helper.setText(R.id.product_title,"撒大苏打实打实");
        }
    }
}
